package com.top.mall.main.income

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import com.top.mall.R
import com.top.mall.custom.setTransitionStatus
import com.top.mall.jetpack.vm.CouponViewModel
import com.top.mall.main.income.adapter.CouponCashAdapter
import kotlinx.android.synthetic.main.include_app_refresh.*


class CouponCashFragment : OwnerViewModeFragment<CouponViewModel>() {

    companion object {
        const val KEY_COLLECT_ACTION = "KEY_COLLECT_ACTION"
        fun newInstance(action: Int): CouponCashFragment {
            val bundle = Bundle().apply {
                putInt(KEY_COLLECT_ACTION, action)
            }
            return CouponCashFragment().apply {
                arguments = bundle
            }
        }
    }

    private val mCouponCashAdapter = CouponCashAdapter()

    private val mSourceType: Int
        get() {
            val action = arguments?.getInt(KEY_COLLECT_ACTION) ?: 0
            return if (action == 1) 1 else if (action == 2) 3 else 2
        }

    override fun viewModel(): CouponViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.include_app_refresh

    override fun initView() {
        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mCouponCashAdapter

        mMaterialHeader.setColorSchemeResources(R.color.colorMain)
        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.userCoupon(mSourceType)
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.userCoupon(mSourceType)
        }
    }

    override fun initData() {
        viewModel.userCoupon(mSourceType)
    }

    override fun initViewMode() {
        viewModel.userCouponLiveData.observe(viewLifecycleOwner, { response ->
            mSmartRefreshLayout.setTransitionStatus(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.list
                ?: return@observe
            mCouponCashAdapter.setNewInstance(data)
        })
    }
}